home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / gfx / x11 / x3270_3_2_16.lha / amiga_src / charset.c < prev    next >
C/C++ Source or Header  |  2009-02-22  |  4KB  |  166 lines

  1. /*
  2.  * Modifications Copyright 1993, 1994, 1995, 1996, 1999, 2000 by Paul Mattes.
  3.  * Original X11 Port Copyright 1990 by Jeff Sparkes.
  4.  *  Permission to use, copy, modify, and distribute this software and its
  5.  *  documentation for any purpose and without fee is hereby granted,
  6.  *  provided that the above copyright notice appear in all copies and that
  7.  *  both that copyright notice and this permission notice appear in
  8.  *  supporting documentation.
  9.  *
  10.  * Copyright 1989 by Georgia Tech Research Corporation, Atlanta, GA 30332.
  11.  *  All Rights Reserved.  GTRC hereby grants public use of this software.
  12.  *  Derivative works based on this software must incorporate this copyright
  13.  *  notice.
  14.  */
  15.  
  16. /*
  17.  *    charset.c
  18.  *        This module handles character sets.
  19.  */
  20.  
  21. #include "globals.h"
  22.  
  23. #include "resources.h"
  24. #include "appres.h"
  25. #include "cg.h"
  26.  
  27. #include "charsetc.h"
  28. #include "kybdc.h"
  29. #include "tablesc.h"
  30. #include "utilc.h"
  31.  
  32. /* Globals. */
  33. Boolean charset_changed = False;
  34.  
  35. /* Statics. */
  36. static void remap_chars(char *spec);
  37.  
  38. /*
  39.  * Change character sets.
  40.  * Returns True if the new character set was found, False otherwise.
  41.  */
  42. Boolean
  43. charset_init(char *csname)
  44. {
  45.     char *resname;
  46.     char *cs;
  47.  
  48.     if (csname == CN)
  49.         return True;
  50.  
  51.     /* Find the character set definition. */
  52.     resname = xs_buffer("%s.%s", ResCharset, csname);
  53.     cs = get_resource(resname);
  54.     Free(resname);
  55.     if (cs == CN)
  56.         return False;
  57.     if (appres.charset == CN || strcmp(csname, appres.charset)) {
  58.         appres.charset = NewString(csname);
  59.         charset_changed = True;
  60.     }
  61.  
  62.     /* Set up the translation tables. */
  63.     (void) memcpy((char *)ebc2cg, (char *)ebc2cg0, 256);
  64.     (void) memcpy((char *)cg2ebc, (char *)cg2ebc0, 256);
  65. #if defined(X3270_FT) /*[*/
  66.     (void) memcpy((char *)ft2asc, (char *)ft2asc0, 256);
  67.     (void) memcpy((char *)asc2ft, (char *)asc2ft0, 256);
  68. #endif /*]*/
  69.     clear_xks();
  70.     remap_chars(NewString(cs));
  71.     return True;
  72. }
  73.  
  74. /*
  75.  * Map a keysym name or literal string into a character.
  76.  * Returns NoSymbol if there is a problem.
  77.  */
  78. static KeySym
  79. parse_keysym(char *s, Boolean extended)
  80. {
  81.     KeySym    k;
  82.  
  83.     k = StringToKeysym(s);
  84.     if (k == NoSymbol) {
  85.         if (strlen(s) == 1)
  86.             k = *s & 0xff;
  87.         else
  88.             return NoSymbol;
  89.     }
  90.     if (k < ' ' || (!extended && k > 0xff))
  91.         return NoSymbol;
  92.     else
  93.         return k;
  94. }
  95.  
  96. /*
  97.  * Parse an EBCDIC character set map, a series of pairs of numeric EBCDIC codes
  98.  * and keysyms.
  99.  *
  100.  * If the keysym is in the range 1..255, it is a remapping of the EBCDIC code
  101.  * for a standard Latin-1 graphic, and the CG-to-EBCDIC map will be modified
  102.  * to match.
  103.  *
  104.  * Otherwise (keysym > 255), it is a definition for the EBCDIC code to use for
  105.  * a multibyte keysym.  This is intended for 8-bit fonts that with special
  106.  * characters that replace certain standard Latin-1 graphics.  The keysym
  107.  * will be entered into the extended keysym translation table.
  108.  */
  109. static void
  110. remap_chars(char *spec)
  111. {
  112.     char    *s;
  113.     char    *ebcs, *isos;
  114.     unsigned char    ebc, cg;
  115.     KeySym    iso;
  116.     int    ne = 0;
  117.     int    ns;
  118.  
  119.     /* Pick apart a copy of the spec. */
  120.     s = spec = NewString(spec);
  121.  
  122.     while ((ns = split_dresource(&s, &ebcs, &isos))) {
  123.         ne++;
  124.         if (ns < 0 ||
  125.             !(ebc = strtol(ebcs, (char **)NULL, 0)) ||
  126.             (iso = parse_keysym(isos, True)) == NoSymbol) {
  127.             char    nbuf[16];
  128.  
  129.             (void) sprintf(nbuf, "%d", ne);
  130.             xs_warning("Cannot parse %s \"%s\", entry %s",
  131.                 ResCharset, appres.charset, nbuf);
  132.             break;
  133.         }
  134.         if (iso <= 0xff) {
  135. #if defined(X3270_FT) /*[*/
  136.             unsigned char aa;
  137. #endif /*]*/
  138.  
  139.             cg = asc2cg[iso];
  140.             if (cg2asc[cg] == iso) {    /* well-defined */
  141.                 ebc2cg[ebc] = cg;
  142.                 cg2ebc[cg] = ebc;
  143.             } else {            /* into a hole */
  144.                 ebc2cg[ebc] = CG_boxsolid;
  145.             }
  146. #if defined(X3270_FT) /*[*/
  147.             /*
  148.              * Change the file transfer translation table as well.
  149.              * This works properly with the US-English character
  150.              * set and the bracket character set (which are the
  151.              * same, as far as the host is concerned), but has not
  152.              * yet been tried out with hosts that genuinely think
  153.              * they are using a non-English character set.
  154.              */
  155.             aa = asc2ft[ebc2asc[ebc]];
  156.             ft2asc[aa] = iso;
  157.             asc2ft[iso] = aa;
  158. #endif /*]*/
  159.         } else {
  160.             add_xk(iso, (KeySym)cg2asc[ebc2cg[ebc]]);
  161.         }
  162.  
  163.     }
  164.     Free(spec);
  165. }
  166.